﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerGame.Manager;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using ServerBase.Other;
using ServerPublic.Data;
using ServerPublic.DataBaseMysql;
using static UtilPublic.Redis.ServerRedis;
using static UtilPublic.Serialization.Jsh;
using static ServerBase.DataBaseRedis.RdsTxt;
using ServerBase.Service;
using ServerBase.Protocol;
using ServerBase.DataBaseRedis;
using UtilPublic.ServerLoger;
using ServerBase.Config;
using System.Linq;
using UtilPublic.Timer;
using ServerGame.Data;
using UtilPublic.Serialization;

namespace ServerGame.Service
{
    /// <summary>
    /// 服务器信息
    /// </summary>
    public static class ServerMgrGame
    {
        // 服务器存档
        public static void ServerSave(bool 强制 = true)
        {
            if (强制)
            {
                Util.BenchmarkStart($"所有玩家存档 {DataGame.DictPlayerOnline.Count}");
                foreach (var p in DataGame.DictPlayerOnline.Values)
                {
                    p.AutoSave();
                }
                Util.BenchmarkEnd();
            }

            //int 延迟 = DataGame.DictPlayerOnline.Count * 10;
            //延迟 = Math.Max(延迟, 5000);
            //System.Threading.Thread.Sleep(延迟);

            Util.BenchmarkStart("服务器存档");

            PubDataServerMgr.SaveData();
            if (强制)
            {
                //全局数据存储
                DataGame.SaveAll();
            }


            Util.BenchmarkEnd();
        }
        public static void ServerAutoSave()
        {
            ServerSave(false);
        }

        // 关闭分区服务器
        public static void ServerExitPre()
        {
            var Rsp = new G2C_Login_ServerExit();

            Dispatcher.Boardcast(Rsp);

            ServerTimer.StartCalloutSecond(10, ServerExit);
        }
        public static void ServerExit(params object[] args)
        {
            LoadRemove();

            //标记离线时间
            var DtNow = DateTime.Now;
            foreach (var p in DataGame.DictPlayerOnline.Values)
            {
                p.DaMisc.TicksLastLogout = DtNow.Ticks;
            }

            ServerSave(true);

            System.Threading.Thread.Sleep(1000);
            Process.GetCurrentProcess().Kill();
        }

        #region 负载更新
        public static void LoadUpdate()
        {
            var 在线人数 = DataGame.DictPlayerOnline.Values.Count;
            //根据服务器ID和分区设置窗口标题
            Console.Title = $"{BaseServerInfo.ServerZone}-{在线人数}";

            //todo~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 临时修改标题
            Console.Title = "国战服务器调试";

            var info = new CLS_ServerLoadInfo()
            {
                ConfigId = BaseServerInfo.ConfigId,
                ServerIp = BaseServerInfo.ServerIp,
                ServerPort = BaseServerInfo.ServerPort,
                ServerState = EServerState.开启,
                CountOnline = 在线人数,
                ProcessId = BaseServerInfo.ServerCurrentProcessID,
                Cpu = BaseServerInfo.ServerCurrentProcessCpu,
                Memory = BaseServerInfo.ServerCurrentProcessMemory,
                Threads = BaseServerInfo.ServerCurrentProcessThreads,
            };
            ASetAdd(RdsLoadGameSet, info.ConfigId);
            AKvSetTs($"{RdsLoadGameKv}:{info.ConfigId}", Jsh.Jstr(info), TimeSpan.FromSeconds(75));
        }
        public static void LoadRemove()
        {
            ASetRemove(RdsLoadGameSet, BaseServerInfo.ConfigId);
        }
        #endregion
    }

}